package com.tz.farm_feeding.controller;



import java.util.List;

 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.tz.farm_feeding.domain.Batch;
import com.tz.farm_feeding.domain.BatchExample;
import com.tz.farm_feeding.domain.Feed;
import com.tz.farm_feeding.domain.FeedExample;
import com.tz.farm_feeding.domain.Pig;
import com.tz.farm_feeding.domain.PigExample;
import com.tz.farm_feeding.domain.Batch;
import com.tz.farm_feeding.domain.BatchExample;
import com.tz.farm_feeding.domain.status;
import com.tz.farm_feeding.mapper.BatchMapper;
import com.tz.farm_feeding.mapper.FeedMapper;
import com.tz.farm_feeding.mapper.PigMapper;
import com.tz.farm_feeding.mapper.BatchMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@RestController
@RequestMapping("/batch")
public class BatchController {

  @Autowired
  PigMapper pigMapper;

  @Autowired
  BatchMapper batchMapper;
  @PostMapping(value = "/getAllBatch")
  public String getAllBatch(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject resp = new JSONObject();
    List<Batch> batchs = batchMapper.selectByExample(new BatchExample());
    JSONArray jsonArray = new JSONArray();
    for (Batch batch : batchs) {
      jsonArray.add(batch);
    }
    resp.put("result", status.s_ok);
    resp.put("data", jsonArray);
    return resp.toJSONString();
  }
  @PostMapping(value = "/addBatch")
  public String addBatch(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject resp = new JSONObject();
    Batch batch=  new Batch();
    batch.setInfo(jsonparm.getString("info"));
    batch.setName(jsonparm.getString("name"));
    batchMapper.insert(batch);
    resp.put("result", status.s_ok);
    return resp.toJSONString();
  }
 
  @PostMapping(value = "/updateBatch")
  public String updateBatch(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject resp = new JSONObject();
    String info = jsonparm.getString("info");

    String name = jsonparm.getString("name");
    int id = jsonparm.getInteger("id");
    
    Batch batch=  new Batch();
    batch.setInfo(info);
    batch.setName(name);
    BatchExample bExample = new BatchExample();
    BatchExample.Criteria criteria = bExample.createCriteria();
    criteria.andIdEqualTo(id);
    batchMapper.updateByExampleSelective(batch, bExample);
    resp.put("result", status.s_ok);
 
    return resp.toJSONString();
  }

  @PostMapping(value = "/deleteBatch")
  public String deleteBatch(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject resp = new JSONObject();
    int batchid = jsonparm.getInteger("id");
    PigExample example = new PigExample();
    PigExample.Criteria criteria = example.createCriteria();
    criteria.andBatchidEqualTo(batchid);
    List<Pig> pigs = pigMapper.selectByExample(example);
    if( pigs.size() > 0 ){
      resp.put("result", status.s_error);
      return resp.toJSONString();
    }

    BatchExample batchExample = new BatchExample();
    BatchExample.Criteria bCriteria = batchExample.createCriteria();
    bCriteria.andIdEqualTo(batchid);
     batchMapper.deleteByExample(batchExample);
    resp.put("result", status.s_ok);
    
    return resp.toJSONString();
  }

   
}

